package com.hs.conc.thread;

/**
 * @Author: songhao
 * @Date: 2021/8/28
 * @Description:
 * 方法：通过isAlive判断子线程是否活着方式实现
 */
public class MyThread5 extends Thread{

    public static void main(String[] args) throws Exception{
        long start=System.currentTimeMillis();

        //创建一个线程，并进行阻塞
        MyThread5 myThread = new MyThread5();
        myThread.start();

        //如果子线程活着就等待
        while(myThread.isAlive()){
        }

        int result = GlobalValue.result;

        // 确保  拿到result 并输出
        System.out.println("计算结果为：" + result);
        System.out.println("使用时间："+ (System.currentTimeMillis()-start) + " ms");
    }

    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        GlobalValue.result = sum();
    }

    private static int sum() {
        return fibo(36);
    }

    private static int fibo(int a) {
        if ( a < 2)
            return 1;
        return fibo(a-1) + fibo(a-2);
    }
}
